package com.sgcc.pda.data.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;

import com.sgcc.pda.data.IDBH5Manager;
import com.sgcc.pda.data.util.db.DBH5Util;
import com.sgcc.pda.mdrh.receiver.SystemExitReceiver;

import java.util.List;
import java.util.Map;


/**
 * @author superFan
 * date:: 2019/7/17/017 16:44
 * company:: beijing_hezhongweiqi
 * description:: 提供给H5业务应用使用的数据库操作类
 */
public class DBH5Service extends Service {
    private DBH5Binder mBinder;
    // 系统退出广播接收器
    private SystemExitReceiver mSystemExitReceiver;

    @Override
    public void onCreate() {
        super.onCreate();
        mBinder = new DBH5Binder();
        mSystemExitReceiver = SystemExitReceiver.registerReceiver(this);
    }

    @Override
    public void onDestroy() {
        SystemExitReceiver.unregisterReceiver(this, mSystemExitReceiver);
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }


    public class DBH5Binder extends IDBH5Manager.Stub {

        @Override
        public String initDatabase(String dbName, String dbEncrypPwd, int version) throws RemoteException {
            return DBH5Util.getInstance().initDatabase(dbName, dbEncrypPwd, version);
        }

        @Override
        public String create(String dbName, String tableName, Map tableMap) throws RemoteException {
            return DBH5Util.getInstance().create(dbName, tableName, tableMap);
        }

        @Override
        public String insertOne(String dbName, String tableName, String privateKey, Map dataMap) throws RemoteException {
            return DBH5Util.getInstance().insertOne(dbName, tableName, privateKey, dataMap);
        }

        @Override
        public String insertList(String dbName, String tableName, String privateKey, String dataMapListStr) throws RemoteException {
            return DBH5Util.getInstance().insertList(dbName, tableName, privateKey, dataMapListStr);
        }

        @Override
        public String dropTable(String dbName, String tableName) throws RemoteException {
            return DBH5Util.getInstance().dropTable(dbName, tableName);
        }

        @Override
        public String clear(String dbName, String tableName) throws RemoteException {
            return DBH5Util.getInstance().clear(dbName, tableName);
        }

        @Override
        public String update(String dbName, String tableName, Map dataMap, String whereCondition) throws RemoteException {
            return DBH5Util.getInstance().update(dbName, tableName, dataMap, whereCondition);
        }

        @Override
        public String query(String dbName, String tableName, List<String> selectColumnNames, String whereCondition, List<String> groupByColumnNames, String haveingCondition, String orderByCondition, String limitCondition) throws RemoteException {
            return DBH5Util.getInstance().query(dbName, tableName, selectColumnNames, whereCondition,
                    groupByColumnNames, haveingCondition, orderByCondition, limitCondition);
        }

        @Override
        public String joinQuery(String dbName, List<String> tableNames, List<String> selectColumnNames, List<String> whereConditions, List<String> groupByColumnNames, String haveingCondition, String orderByCondition, String limitCondition) throws RemoteException {
            return DBH5Util.getInstance().joinQuery(dbName, tableNames, selectColumnNames, whereConditions, groupByColumnNames, haveingCondition, orderByCondition, limitCondition);
        }

        @Override
        public String delete(String dbName, String tableName, String whereCondition) throws RemoteException {
            return DBH5Util.getInstance().delete(dbName, tableName, whereCondition);
        }

        @Override
        public String rawQuerySQL(String dbName, String sqlStr) throws RemoteException {
            return DBH5Util.getInstance().rawQuerySql(dbName, sqlStr);
        }

        @Override
        public String rawExecSQL(String dbName, String sqlStr) throws RemoteException {
            return DBH5Util.getInstance().rawQuerySql(dbName, sqlStr);
        }

        @Override
        public String getDBInfo(String dbName) throws RemoteException {
            return DBH5Util.getInstance().getDBInfo(dbName);
        }

        @Override
        public String getTableStructureInfo(String dbName, String tableName) throws RemoteException {
            return DBH5Util.getInstance().getTableStructureInfo(dbName, tableName);
        }

        @Override
        public String isTableExist(String dbName, String tableName) throws RemoteException {
            return DBH5Util.getInstance().isTableExist(dbName, tableName);
        }

        @Override
        public String reNameTable(String dbName, String oldTableName, String newTableName) throws RemoteException {
            return DBH5Util.getInstance().reNameTable(dbName, oldTableName, newTableName);
        }

        @Override
        public String addColumn(String dbName, String tableName, Map addColumnMap) throws RemoteException {
            return DBH5Util.getInstance().addColumn(dbName, tableName, addColumnMap);
        }

        @Override
        public String transaction(String dbName, List<String> sqlArray) throws RemoteException {
            return DBH5Util.getInstance().transaction(dbName, sqlArray);
        }

        @Override
        public String closeDatabase(String dbName) throws RemoteException {
            return DBH5Util.getInstance().closeDatabase(dbName);
        }

        @Override
        public String closeAllSqliteHelper() throws RemoteException {
            return DBH5Util.getInstance().closeAllSqliteHelper();
        }
    }

}
